library ItemUtils initializer init
//@author nova, azazel_

    globals
        private constant integer SLOT_FILLER = 'bzbe' 
        private item array dummies

        private real ItemHideX
        private real ItemHideY
    endglobals
    
    private function init takes nothing returns nothing
        local integer i = 0
        set ItemHideX = 0 // GetRectMinX(bj_mapInitialPlayableArea)
        set ItemHideY = 0 // GetRectMinY(bj_mapInitialPlayableArea)
        
        loop
            exitwhen i > 4
            set dummies[i] = CreateItem( SLOT_FILLER , ItemHideX, ItemHideY )
            call SetItemVisible(dummies[i],false)
            set i = i + 1
        endloop
    endfunction
    
    function GetItemSlot takes unit u, item I returns integer
        local integer i = 0 
        loop
            exitwhen i > 5
                if UnitItemInSlot(u,i) == I then
                    return i
                endif
            set i = i + 1
        endloop
        return 7
    endfunction
    
    function GetItemSlotById takes unit u, integer id returns integer
        local integer i = 0
        loop
            exitwhen i > 5
                if GetItemTypeId(UnitItemInSlot(u,i)) == id then
                    return i
                endif
            set i = i + 1
        endloop
        return 7
    endfunction
 
    function UnitAddItemToSlot takes unit whichUnit, item whichItem, integer itemSlot returns boolean
        local boolean rval = false
        local item item0 = UnitItemInSlot(whichUnit,0)
        local item item1 = UnitItemInSlot(whichUnit,1)
        local item item2 = UnitItemInSlot(whichUnit,2)
        local item item3 = UnitItemInSlot(whichUnit,3)
        local item item4 = UnitItemInSlot(whichUnit,4)
        local item item5 = UnitItemInSlot(whichUnit,5)
        
        call SetItemVisible(dummies[0],true)
        call SetItemVisible(dummies[1],true)
        call SetItemVisible(dummies[2],true)
        call SetItemVisible(dummies[3],true)
        call SetItemVisible(dummies[4],true)
        
        if(itemSlot == 0 and item0 == null) then
            call UnitAddItem(whichUnit,whichItem)
            set rval = true
            
        elseif(itemSlot == 1 and item1 == null) then
            if(item0 == null) then
                call UnitAddItem(whichUnit,dummies[0])   
            endif
            call UnitAddItem(whichUnit,whichItem)
            call UnitRemoveItem(whichUnit,dummies[0])
            set rval = true
            
        elseif (itemSlot == 2 and item2 == null) then
            if(item0 == null) then
                call UnitAddItem(whichUnit,dummies[0])   
            endif
             if(item1 == null) then
                call UnitAddItem(whichUnit,dummies[1])   
            endif
            call UnitAddItem(whichUnit,whichItem)
            call UnitRemoveItem(whichUnit,dummies[0])
            call UnitRemoveItem(whichUnit,dummies[1])

            set rval = true
        elseif (itemSlot == 3 and item3 == null) then
            if(item0 == null) then
                call UnitAddItem(whichUnit,dummies[0])   
            endif
            if(item1 == null) then
                call UnitAddItem(whichUnit,dummies[1])   
            endif
            if(item2 == null) then
                call UnitAddItem(whichUnit,dummies[2])   
            endif
            
            call UnitAddItem(whichUnit,whichItem)
            call UnitRemoveItem(whichUnit,dummies[0])
            call UnitRemoveItem(whichUnit,dummies[1])
            call UnitRemoveItem(whichUnit,dummies[2])
            set rval = true
        elseif (itemSlot == 4 and item4 == null) then
            if(item0 == null) then
                call UnitAddItem(whichUnit,dummies[0])   
            endif
            if(item1 == null) then
                call UnitAddItem(whichUnit,dummies[1])   
            endif
            if(item2 == null) then
                call UnitAddItem(whichUnit,dummies[2])   
            endif
            if(item3 == null) then
                call UnitAddItem(whichUnit,dummies[3])   
            endif
            
            call UnitAddItem(whichUnit,whichItem)
            call UnitRemoveItem(whichUnit,dummies[0])
            call UnitRemoveItem(whichUnit,dummies[1])
            call UnitRemoveItem(whichUnit,dummies[2])
            call UnitRemoveItem(whichUnit,dummies[3])
            set rval = true
            
        elseif (itemSlot == 5 and item5 == null) then
            if(item0 == null) then
                call UnitAddItem(whichUnit,dummies[0])   
            endif
            if(item1 == null) then
                call UnitAddItem(whichUnit,dummies[1])   
            endif
            if(item2 == null) then
                call UnitAddItem(whichUnit,dummies[2])   
            endif
            if(item3 == null) then
                call UnitAddItem(whichUnit,dummies[3])   
            endif
            if(item4 == null) then
                call UnitAddItem(whichUnit,dummies[4])   
            endif
            
            call UnitAddItem(whichUnit,whichItem)
            call UnitRemoveItem(whichUnit,dummies[0])
            call UnitRemoveItem(whichUnit,dummies[1])
            call UnitRemoveItem(whichUnit,dummies[2])
            call UnitRemoveItem(whichUnit,dummies[3])
            call UnitRemoveItem(whichUnit,dummies[4])
            set rval = true
        endif
        call SetItemVisible(dummies[0],false)
        call SetItemVisible(dummies[1],false)
        call SetItemVisible(dummies[2],false)
        call SetItemVisible(dummies[3],false)
        call SetItemVisible(dummies[4],false)
        set item0 = null
        set item1 = null
        set item2 = null
        set item3 = null
        set item4 = null
        set item5 = null
        return rval
    endfunction
    
    
    function InItemList takes integer itemid, string itemlist returns boolean
       // return GetB(itemlist,itemid)
       return true
    endfunction

    function HasUnitItem takes unit u, integer itemid, string itemlist returns boolean
        local integer I = 0
        local integer itemcount = 0
        loop
            exitwhen I==6
            if InItemList(GetItemTypeId(UnitItemInSlot(u, I)),itemlist) then
                set itemcount = itemcount + 1
                if itemcount>1 then
                    return true
                endif
            endif
            set I = I + 1
        endloop
        return InItemList(GetUnitTypeId(u),itemlist)
    endfunction

    function CanPickup takes unit u, integer itemid, string itemlist returns boolean
        return InItemList(itemid,itemlist) and HasUnitItem(u,itemid,itemlist)
    endfunction

    function GetUnitItem takes unit u, integer itemId returns item
        local integer slot = 0
        local item i
        loop
            set i = UnitItemInSlot(u,slot)
            if i!=null and GetItemTypeId(i)==itemId then
                exitwhen 1==1
            endif
            set slot = slot + 1
            exitwhen slot == 6
        endloop
        if i!=null then
            set i = null
        endif
        if slot==6 then
            return null
        else
            return UnitItemInSlot(u,slot)
        endif
    endfunction

    function StackItem takes unit u, item pick, integer itemid, integer box, integer single, integer max returns boolean
        local item inv
        local integer n
        local integer n2
        local integer inven = 0
        local boolean stacked = false
        if itemid==single and GetUnitItem(u,box)!=null then  //single was picked up
            loop
                exitwhen inven>5
                set inv = UnitItemInSlot(u,inven)
                set n = GetItemCharges(inv) 
                set n2 = GetItemCharges(pick)
                if GetItemTypeId(inv)==box and n<max then
                    if n2+n<=max then
                        call SetItemCharges(inv,GetItemCharges(inv)+(GetItemCharges(pick)) )
                        call RemoveItem(pick)
                        set inven = 1337
                    else
                        call SetItemCharges(inv,max)
                        call SetItemCharges(pick,n2-(max-n))
                    endif
                    set stacked = true
                endif
                set inven=inven+1
            endloop
        elseif itemid==box then  //box was picked up
            set n = GetItemCharges(pick)
            if n<max then
                loop
                    set inv = GetUnitItem(u,single)
                    exitwhen inv==null or n>=max
                    set n2 = GetItemCharges(inv)
                    if n2+n<=max then
                        call SetItemCharges(pick,GetItemCharges(pick)+n2)
                        call RemoveItem(inv)
                    else
                        call SetItemCharges(pick,max)
                        call SetItemCharges(inv,n2-(max-n))
                    endif
                    set n = GetItemCharges(pick)
                endloop
                set stacked = true
            endif
        endif
        set inv = null
        return stacked
    endfunction

    function StackSame takes unit u, item pickedup, integer box, integer max returns boolean
        local integer I = 0
        local integer n
        local item titem
        if GetItemTypeId(pickedup)!=box then
            return false
        endif
        loop
            exitwhen I > 5
            set titem = UnitItemInSlot(u,I)
            if GetItemTypeId(titem)==box and titem!=pickedup then
                set n = GetItemCharges(pickedup)+GetItemCharges(titem)
                if n<=max then
                    call SetItemCharges(titem,n)
                    call RemoveItem(pickedup)
                    set titem = null
                    return true
                endif
            endif
            set I = I + 1
        endloop
        set titem = null
        return false
    endfunction

endlibrary